package edu.chl.da.ejb.core;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * This class respresent a flight reservation in the system. Uses reservationPK as primary key.
 * @author Jakob
 */
@Entity
@Table(name = "RESERVATION")
@NamedQueries({
    @NamedQuery(name = "Reservation.findAll", query = "SELECT r FROM Reservation r"),
    @NamedQuery(name = "Reservation.findByFromairport", query = "SELECT r FROM Reservation r WHERE r.reservationPK.fromairport = :fromairport"),
    @NamedQuery(name = "Reservation.findByFromdestination", query = "SELECT r FROM Reservation r WHERE r.reservationPK.fromdestination = :fromdestination"),
    @NamedQuery(name = "Reservation.findByAirplane", query = "SELECT r FROM Reservation r WHERE r.reservationPK.airplane = :airplane"),
    @NamedQuery(name = "Reservation.findBySeatno", query = "SELECT r FROM Reservation r WHERE r.reservationPK.seatno = :seatno"),
    @NamedQuery(name = "Reservation.findBySeatrow", query = "SELECT r FROM Reservation r WHERE r.reservationPK.seatrow = :seatrow"),
    @NamedQuery(name = "Reservation.findByCustomer", query = "SELECT r FROM Reservation r WHERE r.reservationPK.customer = :customer"),
    @NamedQuery(name = "Reservation.findByTimecreated", query = "SELECT r FROM Reservation r WHERE r.timecreated = :timecreated")})
public class Reservation implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected ReservationPK reservationPK;
    @Column(name = "TIMECREATED")
    @Temporal(TemporalType.TIMESTAMP)
    private Date timecreated;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "reservation")
    private List<Hotelreservation> hotelreservationList;
    @JoinColumn(name = "CUSTOMER", referencedColumnName = "ACCOUNT", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Customer customer1;
    @JoinColumns({
        @JoinColumn(name = "FROMAIRPORT", referencedColumnName = "FROMAIRPORT", insertable = false, updatable = false),
        @JoinColumn(name = "FROMDESTINATION", referencedColumnName = "FROMDESTINATION", insertable = false, updatable = false),
        @JoinColumn(name = "AIRPLANE", referencedColumnName = "AIRPLANE", insertable = false, updatable = false)})
    @ManyToOne(optional = false)
    private Flight flight;
    @JoinColumns({
        @JoinColumn(name = "AIRPLANE", referencedColumnName = "AIRPLANE", insertable = false, updatable = false),
        @JoinColumn(name = "SEATNO", referencedColumnName = "SEATNO", insertable = false, updatable = false),
        @JoinColumn(name = "SEATROW", referencedColumnName = "ROW", insertable = false, updatable = false)})
    @ManyToOne(optional = false)
    private Seat seat;

    public Reservation() {
    }

    public Reservation(ReservationPK reservationPK) {
        this.reservationPK = reservationPK;
    }

    public Reservation(String fromairport, String fromdestination, int airplane, int seatno, int seatrow, String customer) {
        this.reservationPK = new ReservationPK(fromairport, fromdestination, airplane, seatno, seatrow, customer);
    }

    public ReservationPK getReservationPK() {
        return reservationPK;
    }

    public void setReservationPK(ReservationPK reservationPK) {
        this.reservationPK = reservationPK;
    }

    public Date getTimecreated() {
        return timecreated;
    }

    public void setTimecreated(Date timecreated) {
        this.timecreated = timecreated;
    }

    public List<Hotelreservation> getHotelreservationList() {
        return hotelreservationList;
    }

    public void setHotelreservationList(List<Hotelreservation> hotelreservationList) {
        this.hotelreservationList = hotelreservationList;
    }

    public Customer getCustomer1() {
        return customer1;
    }

    public void setCustomer1(Customer customer1) {
        this.customer1 = customer1;
    }

    public Flight getFlight() {
        return flight;
    }

    public void setFlight(Flight flight) {
        this.flight = flight;
    }

    public Seat getSeat() {
        return seat;
    }

    public void setSeat(Seat seat) {
        this.seat = seat;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (reservationPK != null ? reservationPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Reservation)) {
            return false;
        }
        Reservation other = (Reservation) object;
        if ((this.reservationPK == null && other.reservationPK != null) || (this.reservationPK != null && !this.reservationPK.equals(other.reservationPK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "edu.chl.da.ejb.core.Reservation[reservationPK=" + reservationPK + "]";
    }

}
